﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using NET4.DataNET4;
using NET4.DataNET4.Repository;
using NET4.Database;

namespace NET4.Programma
{
  public partial class DetailSupplier : Form
  {
    #region Delegates & Events
    public delegate void SupplierChangedHandler(object sender, SupplierEventArgs e);
    public event SupplierChangedHandler SupplierChanged;
    #endregion

    #region Properties
    private Webservice.PostcodeService postcodeService;
    protected Webservice.PostcodeService PostcodeService
    {
      get
      {
        if (this.postcodeService == null) { this.postcodeService = new Webservice.PostcodeService(); }
        return this.postcodeService;
      }
    }

    private Leverancier supplier;
    public Leverancier Supplier
    {
      get
      {
        if (this.supplier == null) { this.supplier = new Leverancier(); }
        return this.supplier;
      }
      set
      {
        this.supplier = value;
      }
    }
    #endregion

    public DetailSupplier()
    {
      InitializeComponent();
    }

    #region Protected methods
    protected void OnSupplierChanged()
    {
      if (this.SupplierChanged != null)
      {
        this.SupplierChanged(this, new SupplierEventArgs(this.Supplier));
      }
    }

    protected void Init()
    {
      this.txtName.Text = this.Supplier.Naam;
      this.txtHouseNumber.Text = this.Supplier.Huisnummer;
      this.txtZipCode.Text = this.Supplier.Postcode;
      this.txtStreet.Text = this.Supplier.Straatnaam;
      this.txtCity.Text = this.Supplier.Plaats;
      this.txtPhoneNumber.Text = this.Supplier.Telefoon;
      this.txtEmail.Text = this.Supplier.Email;
    }

    protected new Boolean Validate()
    {
      Boolean result = true;

      this.epError.Clear();

      if (this.txtName.Text == String.Empty)
      {
        this.epError.SetError(this.txtName, "U heeft geen naam ingevuld!");

        result = false;
      }
      if (this.txtZipCode.Text != String.Empty && !Validation.IsZipCode(this.txtZipCode.Text))
      {
        this.epError.SetError(this.txtZipCode, "U heeft geen geldig postcode ingevuld!");

        result = false;
      }
      if (this.txtPhoneNumber.Text != String.Empty && !Validation.IsPhoneNumber(this.txtPhoneNumber.Text))
      {
        this.epError.SetError(this.txtPhoneNumber, "U heeft geen geldig telefoonnummer ingevuld!");

        result = false;
      }
      if (this.txtEmail.Text == String.Empty || !Validation.IsEmail(this.txtEmail.Text))
      {
        this.epError.SetError(this.txtEmail, "U heeft geen of ongeldige email ingevuld!");

        result = false;
      }

      return result;
    }

    protected void Save()
    {
      this.Supplier.Naam = this.txtName.Text;
      this.Supplier.Huisnummer = this.txtHouseNumber.Text;
      this.Supplier.Postcode = this.txtZipCode.Text;
      this.Supplier.Straatnaam = this.txtStreet.Text;
      this.Supplier.Plaats = this.txtCity.Text;
      this.Supplier.Telefoon = this.txtPhoneNumber.Text;
      this.Supplier.Email = this.txtEmail.Text;

      LeverancierRepository.Instance.Save(this.Supplier);
    }
    #endregion

    #region EventHandlers
    private void DetailSupplier_Load(object sender, EventArgs e)
    {
      this.Init();
    }

    private void txtZipCode_TextChanged(object sender, EventArgs e)
    {
      if (this.txtZipCode.Text.Length == 7)
      {
        try
        {
          Webservice.Address address = this.PostcodeService.GetStreetAndCity(this.txtZipCode.Text);
          if (address != null)
          {
            this.txtStreet.Text = address.Street;
            this.txtCity.Text = address.City;
          }
        }
        catch (Exception) { }
      }
    }
    
    private void btnSave_Click(object sender, EventArgs e)
    {
      if (this.Validate())
      {
        this.Save();

        this.OnSupplierChanged();

        this.Close();
      }
    }
    #endregion

  }

  public class SupplierEventArgs : EventArgs
  {
    private Leverancier leverancier;

    public SupplierEventArgs(Leverancier supplier)
    {
      this.leverancier = supplier;
    }

    public Leverancier Leverancier
    {
      get
      {
        if (this.leverancier == null) { this.leverancier = new Leverancier(); }
        return this.leverancier;
      }
      set
      {
        this.leverancier = new Leverancier();
      }
    }
  }
}
